/*
 * Main.java
 *
 * Created on 2007. m�rcius 25., 14:33
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 *
 * Milyen mdostsokat vgeztem Blintn:
 *  - legjobb tudomsom szerint nem kellene bemeneti fjlokkal szarakodni
 *  - megcsinlom specifikcinak megfelelen (semmi bemeneti fjl meg tbbi szarakods)
 *  - ezrt trom a plyabetltt
 *  - s a parancsbemeneteket
 *
 * TODO:
 *  - Ami most inputreader s system.out, azt j lenne majd trni a kszprogramban, s akkor erre tnyleg
 *    elg lesz egy UI-t csinlni hozz.
 */
package lemmings;

import java.io.*;
import java.util.*;

/**
 * 
 * @author attila A f program
 */
public class Main {
	// A modellt reprezentll osztly, csak egy van belle:
	static private Modell modell = null;

	public static void main(String[] args) throws IOException {
		BufferedReader be = null;
		if (args.length >=1) {
			System.out.println(args[0]+" hasznalata");
			FileInputStream fis = new FileInputStream(args[0]);
			be = new BufferedReader(new InputStreamReader(fis));
		} else {
			System.out.println("stdin hasznalata");
			be = new BufferedReader(new InputStreamReader(System.in));
		}
		boolean ingame;
		KCommand kCmd;
		String lastSelected = null;

		// A program elindul, vrja a bemeneteket
		System.out
				.println("# A lemmings-proto program elindult, vrja a bemeneteket. Nhny sz a kimenetrl:");
		System.out
				.println("# A felhasznlnak szl sorok \"#\" -val kezddnek, a diagnosztikaiak \"@\"-cal, ");
		System.out
				.println("# az UI. esemnyei \":\"-val, a fggvnyhvsok \">\"-val, a tbbi nincs definilva.");
		System.out.println("# j jtkot a newgame parancs kiadsval kezdhet.");

		// A parancsok fogadsa:
		ingame = true;
		do {
			kCmd = new KCommand(be.readLine());
			kCmd.printArgs();
			try {
				// j jtk:
				if (kCmd.getCommand().compareTo("newgame") == 0) {
					if (kCmd.argNum() > 1) {
						modell = new Modell(kCmd.getArgs(1));
					} else {
						System.out
								.println("# A modell gy indul, hogy nem kapott indul nevet, gy \"default\" nvvel jn ltre.");
						modell = new Modell("default");
					}
				}

				// Kilps a gammbl:
				if (kCmd.getCommand().compareTo("exit") == 0) {
					ingame = false;
				}

				// tick
				if (kCmd.getCommand().compareTo("tick") == 0) {
					int n = 1;
					System.out.println(kCmd.argNum());
					if (kCmd.argNum() > 1) {
						try {
							n = Integer.parseInt(kCmd.getArgs(1));
						} catch (NumberFormatException e) {
							n = 1;
						}
					}
					for (int j = 0; j < n; j++) {
						modell.nextStep();
					}

				}

				// click
				if (kCmd.getCommand().compareTo("click") == 0) {
					if (kCmd.argNum() > 2) {
						int cX = 0, cY = 0;
						try {
							cX = Integer.parseInt(kCmd.getArgs(1));
							cY = Integer.parseInt(kCmd.getArgs(2));
						} catch (NumberFormatException e) {
							System.out.println(": A parancs parametere nem szam!");
						}
						if (lastSelected != null) {
							if (lastSelected.compareTo("block") == 0) {
								if (GetStoned.getNum() > 0) {
									modell.addTool(cX, cY, new GetStoned());
									GetStoned.setNum(GetStoned.getNum() - 1);
								}
							} else if (lastSelected.compareTo("explode") == 0) {
								if (Explode.getNum() > 0) {
									modell.addTool(cX, cY, new Explode());
									Explode.setNum(Explode.getNum() - 1);
								}
							} else if (lastSelected.compareTo("build") == 0) {
								if (Build.getNum() > 0) {
									modell.addTool(cX, cY, new Build());
									Build.setNum(Build.getNum() - 1);
								}
							} else if (lastSelected.compareTo("drill") == 0) {
								if (Drill.getNum() > 0) {
									modell.addTool(cX, cY, new Drill());
									Drill.setNum(Drill.getNum() - 1);
								}
							} else if (lastSelected.compareTo("para") == 0) {
								if (Parachute.getNum() > 0) {
									modell.addTool(cX, cY, new Parachute());
									Parachute.setNum(Parachute.getNum() - 1);
								}
							}
							lastSelected = null;
						}
					}
				}

				// Eszkz:
				if (kCmd.getCommand().compareTo("block") == 0) {
					lastSelected = new String("block");
					System.out.println("@ Eszkz kivlasztva");
				}
				if (kCmd.getCommand().compareTo("build") == 0) {
					lastSelected = new String("build");
					System.out.println("@ Eszkz kivlasztva");
				}
				if (kCmd.getCommand().compareTo("drill") == 0) {
					lastSelected = new String("drill");
					System.out.println("@ Eszkz kivlasztva");
				}
				if (kCmd.getCommand().compareTo("explode") == 0) {
					lastSelected = new String("explode");
					System.out.println("@ Eszkz kivlasztva");
				}
				if (kCmd.getCommand().compareTo("para") == 0) {
					lastSelected = new String("para");
					System.out.println("@ Eszkz kivlasztva");
				}

			} catch (Exception e) {
				ingame = false;
				System.out.println(e.getMessage());
			}
		} while (ingame);

		System.out.println("# A jtk exitlt.");
		return;
	}
}
